package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.skillcinema.databinding.FilmItemBinding
import com.example.skillcinema.databinding.ShowAllFooterBinding
import com.example.skillcinema.db.displaymodels.FilmDisplayModel
import com.example.skillcinema.db.repositories.BaseRepository.Companion.PREVIEW_ITEMS_COUNT
import com.example.skillcinema.viewholders.FilmItemViewHolder
import com.example.skillcinema.viewholders.FootShowAllViewHolder
import com.example.skillcinema.R

class FilmRecyclerViewAdapter(val onFilmClicked: (id: Int) -> Unit, val onAllClicked: () -> Unit) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var values: List<FilmDisplayModel> = listOf()

    fun setData(list: List<FilmDisplayModel>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIEW_TYPE_FILM -> {
                val binding =
                    FilmItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                FilmItemViewHolder(binding)
            }

            VIEW_TYPE_FOOTER -> {
                val binding =
                    ShowAllFooterBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                FootShowAllViewHolder(binding)
            }

            else -> throw IllegalArgumentException("Unknown view type: $viewType")
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == values.size) VIEW_TYPE_FOOTER else VIEW_TYPE_FILM
    }

    override fun getItemCount(): Int =
        if (values.count() >= PREVIEW_ITEMS_COUNT) values.count() + 1 else values.count()

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is FilmItemViewHolder && position < values.size) {
            val item: FilmDisplayModel = values[position]
            with(holder.binding) {
                root.setOnClickListener {
                    onFilmClicked(item.id)
                }
                ratingTextView.text = item.rate.toString()
                ratingTextView.isVisible = item.rate > 0.0f
                nameTextView.text = item.name
                genreTextView.text = item.genre
                Glide.with(previewImageView.context)
                    .load(item.imageUrl)
                    .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                    .error(android.R.drawable.stat_notify_error)
                    .into(previewImageView)
                if (item.isWatched) {
                    previewImageView.foreground =
                        previewImageView.context.getDrawable(R.drawable.gradient_overlay)
                    seenImageView.visibility = View.VISIBLE
                } else {
                    previewImageView.foreground = null
                    seenImageView.visibility = View.GONE
                }
            }
        } else if (holder is FootShowAllViewHolder) {
            holder.binding.showAllImageButton.setOnClickListener {
                onAllClicked()
            }
        }
    }

    companion object {
        private const val VIEW_TYPE_FILM = 0
        private const val VIEW_TYPE_FOOTER = 1
    }
}